package com.system.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.common.Enums.ResponseEnum;
import com.common.exception.CourseException;
import com.common.util.CopyUtil;
import com.common.util.UuidUtil;
import com.core.system.domain.Role;
import com.core.system.domain.RoleResource;
import com.core.system.domain.RoleUser;
import com.core.system.dto.RoleUserDto;
import com.core.system.mapper.RoleResourceMapper;
import com.core.system.mapper.RoleUserMapper;
import com.core.system.service.imp.RoleServiceImp;
import com.system.Excel.RoleExcel;
import com.system.domain.RoleUserListDto;
import com.system.dto.RoleDto;
import com.system.dto.RoleResourceDto;
import com.system.mapper.RoleExMapper;
import com.system.mapper.RoleResourceExMapper;
import com.system.mapper.RoleUserExMapper;
import com.system.service.RoleExService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


@Service
@Primary
public class RoleExServiceImp extends RoleServiceImp implements RoleExService {
    @Autowired
    private RoleExMapper roleMapper;
    @Autowired
    private RoleResourceMapper roleResourceMapper;
    @Autowired
    private RoleUserMapper roleUserMapper;
    @Autowired
    private RoleResourceExMapper roleResourceExMapper;
    @Autowired
    private RoleUserExMapper roleUserExMapper;

    /**
     * 按角色保存资源
     */
    public ResponseEntity saveRoleResource(RoleDto roleDto) {
        String roleId = roleDto.getId();
        List<String> resourceIds = roleDto.getResourceIds();
        // 清空库中所有的当前角色下的记录
        roleResourceMapper.delete(new LambdaQueryWrapper<RoleResource>().eq(RoleResource::getRoleId, roleId));
        List<RoleResourceDto> roleResourceList = new ArrayList<>();
        RoleResourceDto roleResourceDto = null;
        // 保存角色资源
        for (int i = 0; i < resourceIds.size(); i++) {
            roleResourceDto = new RoleResourceDto();
            roleResourceDto.setId(UuidUtil.getShortUuid());
            roleResourceDto.setRoleId(roleId);
            roleResourceDto.setResourceId(resourceIds.get(i));
            roleResourceList.add(roleResourceDto);
        }
        if(!CollectionUtils.isEmpty(roleResourceList)) {
            int i = roleResourceExMapper.insertRoleResourseList(roleResourceList);
            if (i <= 0) {
                throw new CourseException(ResponseEnum.ADD_ERROR);
            }
        }
        return ResponseEntity.ok().build();
    }

    /**
     * 按角色加载资源
     *
     * @param roleId
     */
    public ResponseEntity rolelistResource(String roleId) {
        List<RoleResource> roleResourceList = roleResourceMapper.selectList(
                new LambdaQueryWrapper<RoleResource>()
                        .eq(roleId != "" && roleId != null, RoleResource::getRoleId, roleId));
        List<String> resourceIdList = new ArrayList<>();
        for (int i = 0, l = roleResourceList.size(); i < l; i++) {
            resourceIdList.add(roleResourceList.get(i).getResourceId());
        }
        return ResponseEntity.ok(resourceIdList);
    }

    /**
     * 批量保存角色用户数据
     *
     * @param roleUserListDto
     * @return
     */
    @Transactional(propagation=Propagation.NESTED,isolation = Isolation.READ_COMMITTED)
    public ResponseEntity saveUserRole(RoleUserListDto roleUserListDto) {
        roleUserMapper.delete(new LambdaQueryWrapper<RoleUser>()
                .eq(RoleUser::getRoleId, roleUserListDto.getRoleId()));
        int size = roleUserListDto.getUserIds().size();
        List<String> ids = roleUserListDto.getUserIds();
        List<RoleUserDto> roleUserList = new ArrayList();
        RoleUserDto roleUser = null;
        for (int i = 0; i < size; i++) {
            roleUser = new RoleUserDto();
            roleUser.setId(UuidUtil.getShortUuid());
            roleUser.setRoleId(roleUserListDto.getRoleId());
            roleUser.setUserId(ids.get(i));
            roleUserList.add(roleUser);
        }
        if(!CollectionUtils.isEmpty(roleUserList)) {
            int i = roleUserExMapper.insertList(roleUserList);
            if (i <= 0) {
                throw new CourseException(ResponseEnum.ADD_ERROR);
            }
        }
        return ResponseEntity.ok().build();

    }

    @Override
    public List<RoleExcel> ExportList() {
        List<Role> roles = baseMapper.selectList(new LambdaQueryWrapper<Role>().orderBy(true, false, Role::getCreateTime));
        return CopyUtil.copyList(roles, RoleExcel.class);
    }

    @Override
    public int insertList(List<RoleExcel> roles) {
        return roleMapper.insertList(roles);
    }

}
